﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;

namespace XMM.Controls.Library
{
    /// <summary>
    /// Interaktionslogik für Holster_VideoInfo.xaml
    /// </summary>
    public partial class Holster_VideoInfo : UserControl
    {
        public string SelectedFilePath = String.Empty;

        public Objects.VideoInfo CurrentVideoInfo { get; set; }

        StackPanel spnTracks = new StackPanel();
        BitmapImage bmpTracks = new BitmapImage(new Uri(@"../../images/bin/tracks.png", UriKind.Relative));

        StackPanel spnDefinitionLogo = new StackPanel();

        StackPanel spnSubtitles = new StackPanel();

        public Holster_VideoInfo()
        {
            InitializeComponent();

            CurrentVideoInfo = new Objects.VideoInfo();

            spnTracks.Orientation = Orientation.Horizontal;
            spnSubtitles.Orientation = Orientation.Vertical;
            spnDefinitionLogo.Orientation = Orientation.Horizontal;
        }

        private void btnRefreshVideoInfo_Click(object sender, RoutedEventArgs e)
        {
            AnalyseVideo();

            loadInfo();   
        }

        ProcessingSplash dlgSplashScreen = new ProcessingSplash();

        public void AnalyseVideo()
        {
            if (this.IsEnabled == true)
            {
                if (System.IO.File.Exists(SelectedFilePath))
                {
                    Thread thAnalysis = new Thread(new ThreadStart(doAnalysis));
                    thAnalysis.Start();

                    dlgSplashScreen = new ProcessingSplash();
                    if ((bool)dlgSplashScreen.ShowDialog())
                    {
                        dlgSplashScreen.Close();
                    }
                }
                else
                {
                    MessageBox.Show("Die Datei \"" + SelectedFilePath + "\" wurde nicht gefunden!", "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
        }

        private void doAnalysis()
        {
            Objects.VideoInfo oVideoInfo = new Objects.VideoInfo();
            try
            {
                // Create MediaInfoLib-Reference for the selected video-file.
                MediaInfoLib.MediaInfo oMediaInfo = new MediaInfoLib.MediaInfo();
                oMediaInfo.Open(SelectedFilePath);

                // Get Vertical-Resolution for HD-Level.
                String sTest = sTest = oMediaInfo.Get(MediaInfoLib.StreamKind.Video, 0, "Height");
                int iResolution = int.Parse(sTest);
                if (iResolution == 540)
                    oVideoInfo.HDLevel = 2;
                else if (iResolution == 720)
                    oVideoInfo.HDLevel = 1;
                else if (iResolution == 1080)
                    oVideoInfo.HDLevel = 3;
                else
                    oVideoInfo.HDLevel = 0;

                // Get Codec.
                sTest = oMediaInfo.Get(MediaInfoLib.StreamKind.Video, 0, "Codec");
                oVideoInfo.Codec = sTest;

                // Subtitle-Tracks
                int iCurrentSubtitleTrack = 0;
                while (true)
                {
                    try
                    {
                        string sLanguage = oMediaInfo.Get(MediaInfoLib.StreamKind.Text, iCurrentSubtitleTrack, "Name");

                        if (sLanguage == String.Empty)
                        {
                            break;
                        }
                        else
                        {
                            oVideoInfo.SubtitleLanguages.Add(sLanguage);
                        }
                    }
                    catch (Exception ex)
                    {
                        break;
                    }
                    ++iCurrentSubtitleTrack;
                }

                // Get Video-Track-Count
                int iVideoTrackCount = 0;
                int iCurrentVideoTrack = 0;
                while (true)
                {
                    try
                    {
                        string sTrackCodec = oMediaInfo.Get(MediaInfoLib.StreamKind.Video, iCurrentVideoTrack, "Codec");

                        if (sTrackCodec == String.Empty)
                            break;
                        else
                        {
                            ++iVideoTrackCount;
                        }
                    }
                    catch (Exception ex)
                    {
                        break;
                    }

                    ++iCurrentVideoTrack;
                }

                oVideoInfo.VideoTracks = iVideoTrackCount;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Fehler beim Analysieren der Datei \"" + SelectedFilePath + "\": " + ex.Message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
            }

            this.Dispatcher.Invoke(new killSplashCallback(killSplash));
            this.Dispatcher.Invoke(new setVideoInfoCallback(setVideoInfo), new object[] { oVideoInfo });
            this.Dispatcher.Invoke(new loadInfoCallback(loadInfo));
        }

        private void setVideoInfo(Objects.VideoInfo oInfo)
        {
            CurrentVideoInfo = oInfo;
        }

        private delegate void setVideoInfoCallback(Objects.VideoInfo oInfo);

        private void killSplash()
        {
            if (dlgSplashScreen != null)
            {
                try
                {
                    dlgSplashScreen.Close();
                }
                catch (Exception ex)
                {
                }
            }
        }

        private delegate void killSplashCallback();

        public void loadInfo()
        {
            wrpVideoInfo.Children.Clear();

            if (CurrentVideoInfo != null)
            {
                // Setup Tracks
                spnTracks.Children.Clear();
                Image imgTracks = new Image();
                imgTracks.Width = 22;
                imgTracks.Height = 22;
                imgTracks.Source = bmpTracks;
                spnTracks.Children.Add(imgTracks);
                Label lblTracks = new Label();
                lblTracks.Content = CurrentVideoInfo.VideoTracks.ToString();
                spnTracks.Children.Add(lblTracks);

                // Setup Definition-Logo
                spnDefinitionLogo.Children.Clear();
                Image imgDefinitionLogo = new Image();
                imgDefinitionLogo.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
                imgDefinitionLogo.VerticalAlignment = System.Windows.VerticalAlignment.Top;
                imgDefinitionLogo.Stretch = Stretch.Uniform;
                imgDefinitionLogo.Width = 50;
                string sImageUri = String.Empty;
                switch (CurrentVideoInfo.HDLevel)
                {
                    case 0: // SD
                        sImageUri = "../../images/bin/nohd.png";
                        break;
                    case 1: // 720p
                        sImageUri = "../../images/bin/hd720.png";
                        break;
                    case 2: // 1080i
                        sImageUri = "../../images/bin/hd1080i.png";
                        break;
                    case 3: // 1080p
                        sImageUri = "../../images/bin/hd1080.png";
                        break;
                }
                imgDefinitionLogo.Source = new BitmapImage(new Uri(sImageUri, UriKind.Relative));
                spnDefinitionLogo.Children.Add(imgDefinitionLogo);

                // Setup Subtitle-Info
                spnSubtitles.Children.Clear();
                Label lblSubtitlesHeadline = new Label();
                lblSubtitlesHeadline.Content = "Untertitel";
                lblSubtitlesHeadline.FontSize = 9;
                lblSubtitlesHeadline.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
                lblSubtitlesHeadline.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center;
                spnSubtitles.Children.Add(lblSubtitlesHeadline);

                if (CurrentVideoInfo.SubtitleLanguages.Count > 0)
                {
                    foreach (string sSubtitleLanguage in CurrentVideoInfo.SubtitleLanguages)
                    {
                        Image imgLanguage = new Image();
                        imgLanguage.Width = 20;
                        imgLanguage.Height = 20;
                        imgLanguage.Source = new BitmapImage(new Uri(@"../../images/lang/" + sSubtitleLanguage + ".png", UriKind.Relative));
                        imgLanguage.ToolTip = "Kanal " + CurrentVideoInfo.SubtitleLanguages.IndexOf(sSubtitleLanguage).ToString() + " (" + sSubtitleLanguage + ")";
                    }
                }
                else
                {
                    Label lblNoSubtitles = new Label();
                    lblNoSubtitles.Content = "keine";
                    lblNoSubtitles.FontSize = 9;
                    lblNoSubtitles.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
                    lblNoSubtitles.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center;
                    spnSubtitles.Children.Add(lblNoSubtitles);
                }

                wrpVideoInfo.Children.Add(spnTracks);
                wrpVideoInfo.Children.Add(spnDefinitionLogo);
                wrpVideoInfo.Children.Add(spnSubtitles);
            }
        }

        public delegate void loadInfoCallback();
    }
}
